package com.example.administrator.testxingpiao.ui.login;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.administrator.testxingpiao.R;
import com.example.administrator.testxingpiao.base.BaseActivity;
import com.example.administrator.testxingpiao.bean.User;
import com.example.administrator.testxingpiao.dao.UserDao;
import com.example.administrator.testxingpiao.ui.main.MainActivity;
import com.example.administrator.testxingpiao.ui.register.RegisterActivity;

public class LoginActivity extends BaseActivity {
    private Button mButton;

    private Button mButtonRegister;
    private Context context;
    private Button mButtonLogin;
    private EditText etName;
    private EditText etPass;
    private String name = "";
    private String pass = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        mButtonRegister= (Button) findViewById(R.id.button_register);
        mButtonLogin= (Button) findViewById(R.id.button_login);
        etName = (EditText) findViewById(R.id.edittext_login_name);
        etPass = (EditText) findViewById(R.id.edittext_login_pass);
        mButtonRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });

        mButtonLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initData();
                if(name.length() > 0 && pass.length() > 0){
                    User user = new User();
                    user.setName(name);
                    user.setPwd(pass);
                    User userQuery= UserDao.queryOne(context,user);
                    String pwd = userQuery.getPwd();
                    //pwd:数据库中查询的密码；pass是手输入的
                    if(pass.equals(pwd)){
                        Intent intent=new Intent(LoginActivity.this, MainActivity.class);
                        startActivity(intent);
                        Toast.makeText(LoginActivity.this, "登录成功！", Toast.LENGTH_SHORT).show();
                    }else {
                        Log.i("TAG","登录失败！");
                        Toast.makeText(LoginActivity.this, "密码错误，请重试！", Toast.LENGTH_SHORT).show();
                    }
                }else {
                    Toast.makeText(LoginActivity.this, "用户名或者密码为空！", Toast.LENGTH_SHORT).show();
                }

            }
        });
    }




    private void initData(){
        name = etName.getText().toString().trim();
        pass = etPass.getText().toString().trim();
    }
}
